#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;
const int K = 65;

int n, k;
queue<ll> q[K];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  rep(i, 1, k) {
    int t;
    cin >> t;
    while (t--) {
      ll x;
      cin >> x;
      q[i].push(x);
    }
  }
  ll l = -1, r = -1, ans = 1ll << 32;
  bool tag = true;
  while (tag) {
    l = r = q[1].front();
    rep(i, 1, k) {
      l = min(l, q[i].front());
      r = max(r, q[i].front());
    }
    ans = min(ans, r - l);
    rep(i, 1, k) {
      if (q[i].front() == l) {
        q[i].pop();
        if (q[i].empty()) tag = false;
      }
    }
  }
  cout << ans;
  return 0;
}